
package coms.dataaccess.hibernate;

import coms.dataaccess.dao.PublicationDAO;
import coms.dto.ConferenceItemBooking;
import coms.dto.Publication;
import coms.dto.User;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

public class PublicationDAOImpl extends GenericDAOImpl<Publication, Integer> implements PublicationDAO{

    private Session session;
    @Override
    public List<Publication> search(Publication publication) throws SQLException {
         session = HibernateUtil.getSessionFactory().openSession();
            Criteria criteria = session.createCriteria(Publication.class, "publication");
            if (publication == null)  return criteria.list();
            
        if (publication.getConferenceItem() != null) {
            criteria.createAlias("publication.conferenceItem", "conferenceItem")
                    .add(Restrictions.eq("conferenceItem.id", publication.getConferenceItem().getId()));
        }
        
    if (publication.getFirstAuthor()!= null) {
            criteria.createAlias("publication.firstAuthor", "author")
                    .add(Restrictions.eq("author.id", publication.getFirstAuthor().getId()));
        }
    
        return criteria.list();
    }

    @Override
    public List<Publication> searchByAnyAuthorType(User author) {
           session = HibernateUtil.getSessionFactory().openSession();
            Criteria criteria = session.createCriteria(Publication.class, "publication");
            if (author != null)  {
            criteria.createAlias("publication.firstAuthor", "fauthor").createAlias("publication.coAuthor", "cauthor")
                    .add(Restrictions.or(Restrictions.eq("fauthor.id", author.getId()), Restrictions.eq("cauthor.id", author.getId())));
      //  .add(Restrictions.eq("author.id", author.getId()));
                    }
            List<Publication> anyName = criteria.list();
            System.out.println("^&&&&&&&&&&&&&&&&&&&&&&&" + anyName.size());
        return anyName;
    }
    
}
